package com.pandora.loan.home.web.advice;

import com.pandora.loan.home.exception.AbstractBusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.NativeWebRequest;

import java.util.Map;

/**
 * @author developer
 */
@ControllerAdvice
@Order(1)
public class BusinessExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @ExceptionHandler(value = {AbstractBusinessException.class})
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Map<String, Object> processBusinessRequestException(NativeWebRequest request, AbstractBusinessException e) {
        logger.error("error happen", e);
        return e.getResponse();
    }

}
